<template>
  <view class="forget-container" :class="responsiveClass" :style="getContainerStyle('md')">
    <!-- 顶部导航栏 -->
    <view class="navbar">
      <view class="navbar-left">
        <view class="back-btn" @click="goBack">
          <text class="back-icon">←</text>
        </view>
      </view>
      <text class="navbar-title">忘记密码</text>
      <view class="navbar-right"></view>
    </view>

    <!-- 主要内容区域 -->
    <view class="main-content">
      <!-- 居中卡片 -->
      <view class="card">
        <view class="card-header">
          <view class="card-icon">🔐</view>
          <view class="card-title">{{ isValid ? '重置密码' : '忘记密码' }}</view>
        </view>
        
        <!-- 第一套表单：账号验证 -->
        <view class="form-section" v-if="!isValid ">
          <!-- 手机号输入 -->
          <view class="input-group">
            <view class="input-label">
              <text class="label-icon">📱</text>
              <text class="label-text">手机号码</text>
            </view>
            <view class="input-container">
              <input 
                class="card-input" 
                placeholder="请输入您的手机号码" 
                type="tel"
                v-model="formData.phone"
                maxlength="11"
              />
            </view>
          </view>
          
          <!-- 邮箱输入 -->
          <view class="input-group">
            <view class="input-label">
              <text class="label-icon">📧</text>
              <text class="label-text">邮箱地址</text>
            </view>
            <view class="input-container">
              <input 
                class="card-input" 
                placeholder="请输入您的邮箱地址" 
                type="email"
                v-model="formData.email"
              />
            </view>
          </view>
          
          <button class="card-btn" @click="nextStep">
            <text class="btn-text">下一步</text>
            <text class="btn-icon">→</text>
          </button>
        </view>

        <!-- 第二套表单：重置密码 -->
        <view class="form-section" v-if="isValid">
          <view class="input-group">
            <view class="input-label">
              <text class="label-icon">🔑</text>
              <text class="label-text">新密码</text>
            </view>
            <view class="input-container">
              <input 
                class="card-input" 
                placeholder="请输入新密码" 
                :type="showPassword ? 'text' : 'password'"
                v-model="pwdForm.NewPassword"
              />
              <view class="password-toggle" @click="togglePassword">
                <text class="toggle-icon">{{ showPassword ? '👁️' : '👁️‍🗨️' }}</text>
              </view>
            </view>
          </view>
          
          <view class="input-group">
            <view class="input-label">
              <text class="label-icon">🔒</text>
              <text class="label-text">确认密码</text>
            </view>
            <view class="input-container">
              <input 
                class="card-input" 
                placeholder="请再次输入新密码" 
                :type="showConfirmPassword ? 'text' : 'password'"
                v-model="pwdForm.ConfirmPassword"
              />
              <view class="password-toggle" @click="toggleConfirmPassword">
                <text class="toggle-icon">{{ showConfirmPassword ? '👁️' : '👁️‍🗨️' }}</text>
              </view>
            </view>
          </view>
          
          <button class="card-btn" @click="resetPassword">
            <text class="btn-text">重置密码</text>
            <text class="btn-icon">✓</text>
          </button>
        </view>

        <view class="card-footer">
          <text class="card-tip">💡 提示：请同时填写邮箱和手机号进行身份验证</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {validateUserInfo,forgetPassword} from '../../api/user.js'
// 当前步骤
const  isValid = ref(false)

// 邮箱和手机号表单
const formData = ref({
  email: 'test@example.com',
  phone: '13800138000'
})
//重置密码表单
let pwdForm=ref({
  NewPassword:"newpassword123",
  ConfirmPassword:"newpassword123"
})
// 密码显示状态
const showPassword = ref(false)
const showConfirmPassword = ref(false)

// 下一步
const nextStep =async () => {
  // 验证邮箱
  const emailValue = formData.value.email.trim()
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  const isEmailValid = emailRegex.test(emailValue)
  
  if (!emailValue) {
    uni.showToast({
      title: '请输入邮箱地址',
      icon: 'none'
    })
    return
  }
  
  if (!isEmailValid) {
    uni.showToast({
      title: '请输入正确的邮箱格式',
      icon: 'none'
    })
    return
  }
  
  // 验证手机号
  const phoneValue = formData.value.phone.trim()
  const phoneRegex = /^1[3-9]\d{9}$/
  const isPhoneValid = phoneRegex.test(phoneValue)
  
  if (!phoneValue) {
    uni.showToast({
      title: '请输入手机号码',
      icon: 'none'
    })
    return
  }
  
  if (!isPhoneValid) {
    uni.showToast({
      title: '请输入正确的手机号格式',
      icon: 'none'
    })
    return
  }
  
  // 显示加载提示 
let {data}=  await validateUserInfo(formData.value)
console.log(data.isValid);
isValid.value=data.isValid
if(data.isValid){

}

}

// 重置密码
const resetPassword =async () => {
  if (!pwdForm.value.NewPassword.trim()) {
    uni.showToast({
      title: '请输入新密码',
      icon: 'none'
    })
    return
  }
  
  if (!pwdForm.value.ConfirmPassword.trim()) {
    uni.showToast({
      title: '请确认密码',
      icon: 'none'
    })
    return
  }
  
  if (pwdForm.value.NewPassword !== pwdForm.value.ConfirmPassword) {
    uni.showToast({
      title: '两次密码不一致',
      icon: 'none'
    })
    return
  }
  
  if (pwdForm.value.NewPassword < 6) {
    uni.showToast({
      title: '密码长度至少6位',
      icon: 'none'
    })
    return
  }
  
  // 显示重置密码提示
 let res=  await forgetPassword(formData.value,pwdForm.value)
 console.log(res);
 

    
    // 显示成功提示
    uni.showToast({
      title: '密码重置成功',
      icon: 'success'
    })
    
    // 跳转到登录页面
  uni.navigateTo({
    url:"/pages/Login/login"
  })
}

// 切换密码显示
const togglePassword = () => {
  showPassword.value = !showPassword.value
}

const toggleConfirmPassword = () => {
  showConfirmPassword.value = !showConfirmPassword.value
}

// 返回登录页面
const goBack = () => {
  console.log('点击返回按钮')
  
  // 优先尝试返回上一页
  uni.navigateTo({
    url:'/pages/Login/login'
  })
}

// 页面加载时输出调试信息
onMounted(() => {
  console.log('忘记密码页面已加载')
  try {
    console.log('当前页面路径:', window?.location?.pathname || '无法获取路径')
  } catch (error) {
    console.log('获取路径失败:', error.message)
  }
})
</script>

<style scoped>
.forget-container {
  min-height: 100vh;
  height: 100vh;
  width: 100%;
  background: #f8f9fa;
  box-sizing: border-box;
  position: relative;
  overflow-x: hidden;
}

/* 顶部导航栏 */
.navbar {
  width: 100%;
  height: calc(48px + env(safe-area-inset-top, 0px));
  background: #fff;
  display: flex;
  align-items: flex-end;
  justify-content: flex-start;
  padding: env(safe-area-inset-top, 0px) 0 0 0;
  box-sizing: border-box;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
}

.navbar-left {
  display: flex;
  align-items: center;
  height: 48px;
  margin-left: 0;
  z-index: 101;
}

.back-btn {
  display: flex;
  align-items: center;
  padding: 0;
  background: none;
  border-radius: 0;
  box-shadow: none;
  cursor: pointer;
  margin: 0;
  height: 48px;
}

.back-icon {
  font-size: 22px;
  color: #222;
  font-weight: 400;
  line-height: 48px;
  user-select: none;
  display: flex;
  align-items: center;
  height: 48px;
}

.navbar-title {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(env(safe-area-inset-top, 0px) + 8px);
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  color: #222;
  pointer-events: none;
  z-index: 100;
}

.navbar-right {
  flex: 1;
}



.logo-container {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.logo {
  font-size: 20px;
  color: white;
}

.brand {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
  letter-spacing: 0.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}



.nav-link {
  margin: 0 12px;
  color: #7f8c8d;
  font-weight: 500;
  transition: color 0.3s ease;
}

.nav-link:hover {
  color: #667eea;
}

.nav-divider {
  margin: 0 8px;
  color: #bdc3c7;
}

/* 主要内容区域 */
.main-content {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(60px + env(safe-area-inset-top, 0px) + 40px) 20px 30px 20px;
  min-height: calc(100vh - 60px - env(safe-area-inset-top, 0px));
}

/* 装饰性背景 - 已移除 */

/* 卡片样式 */
.card {
  width: 100%;
  max-width: 400px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  padding: 32px 24px;
  box-sizing: border-box;
  position: relative;
  z-index: 2;
}

.card-header {
  text-align: center;
  margin-bottom: 24px;
}

.card-icon {
  font-size: 32px;
  margin-bottom: 16px;
  display: block;
  color: #667eea;
}

.card-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
}

.card-desc {
  font-size: 18px;
  color: #7f8c8d;
  line-height: 1.6;
  max-width: 320px;
  margin: 0 auto;
}

/* 表单区域 */
.form-section {
  margin-bottom: 24px;
}



.input-group {
  margin-bottom: 24px;
}

.input-label {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  padding: 0 4px;
}

.label-icon {
  font-size: 18px;
  margin-right: 8px;
  color: #667eea;
  background: rgba(102, 126, 234, 0.1);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.label-text {
  font-size: 15px;
  font-weight: 600;
  color: #333;
  letter-spacing: 0.5px;
}

.input-container {
  position: relative;
  background: #f8f9fa;
  border-radius: 12px;
  padding: 4px;
  transition: all 0.3s ease;
}

.input-container:focus-within {
  background: #ffffff;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.15);
}

.card-input {
  width: 100%;
  height: 52px;
  font-size: 15px;
  border: none;
  border-radius: 8px;
  padding: 0 20px;
  box-sizing: border-box;
  color: #333;
  background: transparent;
  transition: all 0.3s ease;
  font-weight: 500;
}

.card-input:focus {
  outline: none;
  background: #ffffff;
}

.card-input::placeholder {
  color: #999;
  font-weight: 400;
}

/* 密码切换按钮 */
.password-toggle {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  z-index: 10;
  padding: 4px;
  border-radius: 4px;
  transition: all 0.2s ease;
}

.password-toggle:hover {
  background: rgba(102, 126, 234, 0.1);
}

.toggle-icon {
  font-size: 16px;
  color: #999;
  transition: color 0.2s ease;
}

.password-toggle:hover .toggle-icon {
  color: #667eea;
}

/* 按钮样式 */
.card-btn {
  width: 100%;
  height: 56px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.card-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.card-btn:hover::before {
  left: 100%;
}

.card-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.card-btn:active {
  transform: translateY(0);
}

.btn-text {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.btn-icon {
  font-size: 18px;
  transition: transform 0.3s ease;
}

.card-btn:hover .btn-icon {
  transform: translateX(4px);
}

/* 底部提示 */
.card-footer {
  text-align: center;
  margin-top: 20px;
  padding: 16px;
  background: rgba(102, 126, 234, 0.05);
  border-radius: 8px;
  border-left: 3px solid #667eea;
}

.card-tip {
  color: #666;
  font-size: 13px;
  line-height: 1.5;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .card {
    padding: 32px 24px;
    margin: 20px;
  }
  
  .card-title {
    font-size: 28px;
  }
  
  .card-desc {
    font-size: 16px;
  }
  
  .navbar {
    padding: 0 16px;
  }
}

/* 响应式断点样式 */
/* 平板断点 */
@media (min-width: 481px) {
  .forget-container {
    max-width: 480px;
    margin: 0 auto;
  }
  
  .card {
    padding: 40px 32px;
  }
  
  .card-input {
    font-size: 32rpx;
  }
  
  .card-btn {
    height: 88rpx;
    font-size: 36rpx;
  }
}

/* 桌面中等断点 */
@media (min-width: 769px) {
  .forget-container {
    max-width: 520px;
    margin: 0 auto;
  }
  
  .card {
    padding: 48px 40px;
  }
  
  .card-header {
    margin-bottom: 60rpx;
  }
  
  .card-title {
    font-size: 48rpx;
  }
  
  .card-desc {
    font-size: 32rpx;
  }
  
  .input-group {
    margin-bottom: 44rpx;
  }
}

/* 桌面大屏断点 */
@media (min-width: 1025px) {
  .forget-container {
    max-width: 560px;
    margin: 0 auto;
  }
  
  .card {
    padding: 56px 48px;
  }
  
  .input-group {
    margin-bottom: 48rpx;
  }
  
  .card-btn {
    height: 100rpx;
    font-size: 40rpx;
  }
}
</style>
